Code
library(reticulate)Tony Duan
July 6, 2023

recommend to use class() over typeof() to check data type
One number is a vector too.
create vector
append vector
calculate vector
select vector element
select the first one
exclude the first one
select 3 to 5
select bigger than 2 element
Matrix is two dimensions data only with number.
[,1] [,2] [,3] [,4]
[1,] 1 5 9 13
[2,] 2 6 10 14
[3,] 3 7 11 15
[4,] 4 8 12 16
[,1] [,2] [,3] [,4]
[1,] 2 10 18 26
[2,] 4 12 20 28
[3,] 6 14 22 30
[4,] 8 16 24 32
Dataframe is two dimensions data mixed with number and string.
As array is made up matrices in multiple dimensions.
it will break out of the loop when i=2
it will break out of the loop when order=4
http://adv-r.had.co.nz/Data-structures.html#vectors
https://yards.albert-rapp.de/
Loops using R programming: https://www.youtube.com/watch?v=UvopClD98LQ
---
title: "data type and data structure in R"
author: "Tony Duan"
date: "2023-07-06"
categories: [R]
execute:
warning: false
error: false
format:
html:
toc: true
toc-location: left
code-fold: show
code-tools: true
number-sections: true
code-block-bg: true
code-block-border-left: "#31BAE9"
---
{width="488"}
```{r}
library(reticulate)
```
# data type
## character
recommend to use class() over typeof() to check data type
```{r}
x <- "dataset"
class(x)
```
## numeric
```{r}
x <- 123
class(x)
```
## complex
```{r}
x <- 3 + 2i
class(x)
```
```{r}
x <- TRUE
class(x)
```
# Data structure
## Vector
One number is a vector too.
```{r}
y <- 1
y
class(y)
```
```{r}
y <- c(1,2,3)
y
class(y)
```
create vector
```{r}
seq(from = 2, to = 14, by = 2)
```
```{r}
rep(x = 1.5, times = 4)
```
append vector
```{r}
x=c(1,2,3)
y=c(4,5,6)
z=c(x,y)
z
```
calculate vector
```{r}
x=c(1,2,3,4,5)
```
```{r}
sum(x)
```
```{r}
mean(x)
```
```{r}
y=x+10
```
select vector element
```{r}
y
```
select the first one
```{r}
y[1]
# or
head(y,1)
```
exclude the first one
```{r}
y[-1]
```
```{r}
y[length(y)]
# or
tail(y,1)
```
select 3 to 5
```{r}
y[c(3:5)]
```
select bigger than 2 element
```{r}
y[y>2]
```
## Matrix
Matrix is two dimensions data only with number.
```{r}
matrix001 <- matrix(1:16, nrow = 4)
matrix001
```
```{r}
is.matrix(matrix001)
```
```{r}
matrix002=matrix001+matrix001
matrix002
```
### select one element in matrix first row and second column
```{r}
matrix002[1,2]
```
### select second row
```{r}
matrix002[2,]
```
### select third Column
```{r}
matrix002[,3]
```
## Dataframe
Dataframe is two dimensions data mixed with number and string.
```{r}
height <- c(180, 155, 160, 167, 181)
weight <- c(65, 50, 52, 58, 70)
names <- c("Joanna", "Charlotte", "Helen", "Karen", "Amy")
y <- data.frame(height = height, weight = weight, names = names)
class(y)
class(y$height)
class(y$names)
```
## Array
As array is made up matrices in multiple dimensions.
```{r}
# Create two vectors of different lengths.
vector1 <- c(9,1,0)
vector2 <- c(6,0,11,3,14,1,2,6,9)
array2 <- array(c(vector1,vector2),dim = c(3,3,2))
array2
```
## List
```{r}
y <- list(c("black", "yellow", "orange"),
c(TRUE, TRUE, FALSE, TRUE, FALSE, FALSE),
matrix(1:6, nrow = 3))
class(y)
```
# loop
## for loop
```{r}
vector1=c(1,2,3)
for (i in vector1){
cat('number is:',i,'\n')
}
```
## while loop
```{r}
order=6
while (order>3){
print(order)
order=order-1
}
```
## if loop
```{r}
a=16
if (a>10){
print ("a>10")
}else{
print("a<=10")
}
```
## if in for loop:
it will break out of the loop when i=2
```{r}
vector1=c(1,2,3)
for (i in vector1){
print('number is:')
if (i ==2){break}
print(i)
}
```
## if in while loop:
it will break out of the loop when order=4
```{r}
order=8
while (order>2){
print(order)
if (order ==4){break}
order=order-1
}
```
# Reference
http://adv-r.had.co.nz/Data-structures.html#vectors
https://yards.albert-rapp.de/
Loops using R programming:
https://www.youtube.com/watch?v=UvopClD98LQ